<?php
/**
 * RenderImage helper
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://digitalus-media.com/license/new-bsd
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to info@digitalus-media.com so we can send you a copy immediately.
 *
 * @author      Forrest Lyman
 * @category    Digitalus
 * @package     Digitalus_View
 * @subpackage  Helper
 * @copyright   Copyright (c) 2007 - 2009,  Digitalus Media USA (digitalus-media.com)
 * @license     http://digitalus-media.com/license/new-bsd     New BSD License
 * @version     $Id:$
 * @link        http://www.digitaluscms.com
 * @since       Release 1.5.0
 */
/**
 * @see Zend_View_Helper_Abstract
 */
require_once 'Zend/View/Helper/Abstract.php';
/**
 * RenderImage helper
 *
 * @author      Forrest Lyman
 * @copyright   Copyright (c) 2007 - 2009,  Digitalus Media USA (digitalus-media.com)
 * @license     http://digitalus-media.com/license/new-bsd     New BSD License
 * @version     Release: @package_version@
 * @link        http://www.digitaluscms.com
 * @since       Release 1.5.0
 */
class Dig_View_Helper_RenderImage extends Zend_View_Helper_HtmlElement
{
    /**
     * comments
     */
    public function renderImage ($src, $width, $height = null, $attribs = false)
    {
        $absPath = APPLICATION_PATH . '/../public_html' . $src;
        if ($src != '' && is_file($absPath)) {
            $imageSize = getimagesize($absPath);
            $srcWidth = $imageSize[0];
            $srcHeight = $imageSize[1];
            // try to scale it to width first, since this is most likely 
            $widthScale = $width / $srcWidth;
            // now see if the height will fit
            $heightScale = null;
            if ($height != null) {
                $scaledHeight = $srcHeight * $widthScale;
                if ($scaledHeight > $height) {
                    $heightScale = $height / $srcHeight;
                }
            }
            if ($heightScale != null) {
                // scale to height
                $attribs['height'] = $srcHeight * $heightScale;
                $attribs['width'] = $srcWidth * $heightScale;
            } else {
                // scale to width
                $attribs['height'] = $srcHeight * $widthScale;
                $attribs['width'] = $srcWidth * $widthScale;
            }
            $attribs['src'] = $src;
            return "<img width='{$width}' " . $this->_htmlAttribs($attribs) . " />";
        }
    }
}